package cn.jee.config;

import cn.jee.entity.Student;
import cn.jee.entity.Teacher;
import cn.jee.repository.StudentRepository;
import cn.jee.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private TeacherRepository teacherRepository;
    
    @Autowired
    private StudentRepository studentRepository;
    
    @Override
    public void run(String... args) {
        // 检查是否已有数据
        if (teacherRepository.count() > 0 || studentRepository.count() > 0) {
            return;
        }
        
        // 创建教师
        Teacher teacher1 = new Teacher();
        teacher1.setName("张老师");
        teacher1.setMajor("计算机科学");
        teacherRepository.save(teacher1);
        
        Teacher teacher2 = new Teacher();
        teacher2.setName("李老师");
        teacher2.setMajor("软件工程");
        teacherRepository.save(teacher2);
        
        // 创建学生
        Student student1 = new Student();
        student1.setName("学生1");
        student1.setJavaScore(85);
        student1.setTeacher(teacher1);
        studentRepository.save(student1);
        
        Student student2 = new Student();
        student2.setName("学生2");
        student2.setJavaScore(90);
        studentRepository.save(student2);
    }
} 